/*
 * A simple ToDo Manager
 *
 * Copyright (c) 2011 Nosheen Zaza, Manuel Rebollo, Seyedsaeid Mahpeikar and
 * Wo Ke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package todomanager.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;


/**
 * Creates a panel that holds a notebook like background.
 * @author Seyedsaeid Mahpeikar
 * @author Wo Ke
 */
public class Background extends JPanel {

    JTiledPanel centerBackgroundPanel;
    JTiledPanel notePadTopMiddlePanel;
    JTiledPanel forMenuMiddlePanel;
    JTiledPanel innerFrameTopMiddlePanel;
    JPanel innerFrameTopPanel;

    /**
     * Creates an instance of {@code Background} and sets it up.
     * Here we used some small sized graphical elements and
     * tiled panels to make sure that when when window is resized,
     * it still looks like a normal note pad.
     */
    public Background() {
        // Create and set up the window.

        /**
         * Set up the main panels for the background.
         * We have to use a rather complicated 3-layer structure here,
         * to match with the many customized graphical elements.
         */
        JPanel mainBackgroundPanel = new JPanel();
        mainBackgroundPanel.setLayout(new BorderLayout());

        JPanel notePadMainPanel = new JPanel();
        notePadMainPanel.setLayout(new BorderLayout());

        JPanel innerFrameMainPanel = new JPanel();
        innerFrameMainPanel.setLayout(new BorderLayout());


        notePadMainPanel.add(BorderLayout.CENTER, innerFrameMainPanel);
        mainBackgroundPanel.add(BorderLayout.CENTER, notePadMainPanel);

        JPanel notePadTopPanel = new JPanel(new BorderLayout());
        notePadMainPanel.add(BorderLayout.NORTH, notePadTopPanel);

        JPanel forMenuPanel = new JPanel(new BorderLayout());
        mainBackgroundPanel.add(BorderLayout.NORTH, forMenuPanel);

        innerFrameTopPanel = new JPanel(new BorderLayout());
        innerFrameMainPanel.add(BorderLayout.NORTH, innerFrameTopPanel);

        JPanel innerFrameBottomPanel = new JPanel(new BorderLayout());
        innerFrameMainPanel.add(BorderLayout.SOUTH, innerFrameBottomPanel);


        ClassLoader loader = this.getClass().getClassLoader();

        // Create and set up the panel for menubar
        ImagePanel forMenuLeftPanel = new ImagePanel(
                new ImageIcon(loader.getResource(
                "todomanager/resources/ForMenuLeft.png")).getImage());
        forMenuPanel.add(BorderLayout.WEST, forMenuLeftPanel);

        forMenuMiddlePanel = SetSizeTiledPanel(new JTiledPanel(
                new ImageIcon(loader.getResource(
                "todomanager/resources/ForMenuMiddle.png")).getImage()));
        forMenuPanel.add(BorderLayout.CENTER, forMenuMiddlePanel);

        ImagePanel forMenuRightPanel = new ImagePanel(new ImageIcon(
                loader.getResource(
                "todomanager/resources/ForMenuRight.png")).getImage());
        forMenuPanel.add(BorderLayout.EAST, forMenuRightPanel);

        // Create and set up the panel for menubar
        ImagePanel notePadTopLeftPanel = new ImagePanel(
                new ImageIcon(loader.getResource(
                "todomanager/resources/RingsLeft.png")).getImage());
        notePadTopPanel.add(BorderLayout.WEST, notePadTopLeftPanel);

        notePadTopMiddlePanel = SetSizeTiledPanel(new JTiledPanel(
                new ImageIcon(loader.getResource(
                "todomanager/resources/RingsMiddle.png")).getImage()));
        notePadTopPanel.add(BorderLayout.CENTER, notePadTopMiddlePanel);

        ImagePanel notePadTopRightPanel = new ImagePanel(
                new ImageIcon(loader.getResource(
                "todomanager/resources/RingsRight.png")).getImage());
        notePadTopPanel.add(BorderLayout.EAST, notePadTopRightPanel);

        // Create and set up the top panels for the inner frame.
        ImagePanel innerFrameTopLeftPanel = new ImagePanel(
                new ImageIcon(loader.getResource(
                "todomanager/resources/InnerFrameTopLeft.png")).getImage());

        innerFrameTopMiddlePanel = SetSizeTiledPanel(new JTiledPanel(
                new ImageIcon(loader.getResource(
                "todomanager/resources/InnerFrameTopMiddle.png")).getImage()));
        innerFrameTopPanel.add(BorderLayout.CENTER, innerFrameTopMiddlePanel);

        // Create and set up the bottom panels for the inner frame.
        ImagePanel innerFrameBottomLeftPanel = new ImagePanel(
                new ImageIcon(loader.getResource(
                "todomanager/resources/InnerFrameBottomLeft.png")).getImage());
        innerFrameBottomPanel.add(BorderLayout.WEST, innerFrameBottomLeftPanel);

        JTiledPanel innerFrameBottomMiddlePanel = SetSizeTiledPanel(
                new JTiledPanel(new ImageIcon(loader.getResource(
                "todomanager/resources/InnerFrameBottomMiddle.png")).getImage()));
        innerFrameBottomPanel.add(BorderLayout.CENTER, innerFrameBottomMiddlePanel);

        ImagePanel innerFrameBottomRightPanel = new ImagePanel(
                new ImageIcon(loader.getResource(
                "todomanager/resources/InnerFrameBottomRight.png")).getImage());
        innerFrameBottomPanel.add(BorderLayout.EAST, innerFrameBottomRightPanel);

        // Right panel.
        JTiledPanel innerFrameRightPanel = SetSizeTiledPanel(
                new JTiledPanel(new ImageIcon(loader.getResource(
                "todomanager/resources/InnerFrameRight.png")).getImage()));
        innerFrameMainPanel.add(BorderLayout.EAST, innerFrameRightPanel);
        // Left panel.
        JTiledPanel innerFrameLeftPanel = SetSizeTiledPanel(
                new JTiledPanel(new ImageIcon(loader.getResource(
                "todomanager/resources/InnerFrameLeft.png")).getImage()));
        innerFrameMainPanel.add(BorderLayout.WEST, innerFrameLeftPanel);
        // Center panel.
        centerBackgroundPanel = new JTiledPanel(
                new ImageIcon(loader.getResource(
                "todomanager/resources/Center.png")).getImage());
        innerFrameMainPanel.add(BorderLayout.CENTER, centerBackgroundPanel);
        centerBackgroundPanel.setLayout(new BorderLayout());

        this.setLayout(new BorderLayout());
        this.add(mainBackgroundPanel, BorderLayout.CENTER);
    }

    /**
     * Sets the size of the tiled panel
     * @param panel
     * @return
     */
    private JTiledPanel SetSizeTiledPanel(JTiledPanel panel) {
        Image image = panel.getTileImage();
        panel.setPreferredSize(
                new Dimension(image.getWidth(panel), image.getHeight(panel)));
        return panel;
    }

    /**
     * Return a panel so we can add more components on the top of the background.
     * @return A {@code JTiledPanel} reference.
     */
    public JTiledPanel getCenterBackgroundPanel() {
        return centerBackgroundPanel;
    }

    /**
     * Return a panel so we can add more components on the top of the background.
     * @return A {@code JTiledPanel} reference.
     */
    public JTiledPanel getForMenuMiddlePanel() {
        return forMenuMiddlePanel;
    }

    /**
     * Returns the top panel of the inner frame.
     * @return innerFrameTopPanel
     */
    public JPanel getInnerFrameTopPanel() {
        return innerFrameTopPanel;
    }
}
